package com.mxpio.erp.common.service.impl;

import java.util.Date;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums.BpmnStatus;
import com.mxpio.erp.common.enums.OrderStatusEnums.CloseStatus;
import com.mxpio.erp.common.enums.OrderStatusEnums.OrderStatus;
import com.mxpio.erp.common.jpa.model.Order;
import com.mxpio.erp.common.service.OrderService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.security.common.ContextUtils;
import com.mxpioframework.system.service.impl.BaseServiceImpl;

@Service
public class OrderServiceImpl extends BaseServiceImpl<Order> implements OrderService {

	@Override
	@Transactional(readOnly = false)
	public Result<Order> updateBpmnStatus(Order bizOrder, BpmnStatus bpmnStatus) {
		bizOrder.setBpmnStatus(bpmnStatus.getStatusCode());
		bizOrder.setReviewer(ContextUtils.getLoginUsername());
		bizOrder.setReviewTime(new Date());
        this.update(bizOrder);
        return Result.OK();
	}

	@Override
	@Transactional(readOnly = false)
	public Result<Order> updateCloseStatus(Order bizOrder, CloseStatus closeStatus) {
		if(CloseStatus.CLOSED.equals(closeStatus) && bizOrder.getCloseStatus().equals(OrderStatusEnums.CloseStatus.CLOSED.getStatusCode())){
			return Result.error("非打开状态不可关闭");
		}else if(CloseStatus.OPEN.equals(closeStatus) && bizOrder.getCloseStatus().equals(OrderStatusEnums.CloseStatus.OPEN.getStatusCode())){
			return Result.error("非关闭状态不可打开");
		}
		bizOrder.setCloseStatus(closeStatus.getStatusCode());
        this.update(bizOrder);
        return Result.OK(bizOrder);
	}

	@Override
	public Result<? extends Order> updateOrderStatus(Order bizOrder, OrderStatus orderStatus) {
		bizOrder.setOrderStatus(orderStatus.getStatusCode());
        this.update(bizOrder);
        return Result.OK(bizOrder);
	}
	
}
